package cn.zhoutao.org.controller;

import cn.zhoutao.basic.util.AjaxResult;
import cn.zhoutao.basic.util.PageList;
import cn.zhoutao.org.domain.Employee;
import cn.zhoutao.org.query.EmployeeQuery;
import cn.zhoutao.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController//@controller+@responsebody
@RequestMapping("/employ")
@Api(value="部门API",description = "部门相关的CRUD功能")

public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     *查询一条记录
     * @param id
     * @return
     */
    @ApiOperation(value = "通过ID查询一条" )
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }

    /**
     * 高级查询+分页
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }

    /**
     * 新增或者修改
     * @param employee
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdata(@RequestBody Employee employee){
            try {
                if(employee.getId()==null){
                    //新增
                    employeeService.save(employee);
                }else{
                    //修改
                    employeeService.update(employee);
                }
                return AjaxResult.me();
            }catch (Exception e){
                e.printStackTrace();
                return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
            }

    }

    /**
     * 根据id删除一条数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            employeeService.removeById(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("瓦特了！！！");
        }
    }



}
